﻿using System.Collections.Generic;

namespace Sustainalytics.LongRunningTaskManager.Interface
{
    /// <summary>
    /// Status for a given Long Running Task instance (as seen by the Task Manager).
    /// </summary>
    public class LongRunningTaskInstanceStatus : IEqualityComparer<LongRunningTaskInstanceStatus>
    {
        /// <summary>
        /// Task instance ID.
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// Task instance title.
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// Task type.
        /// </summary>
        public LongRunningTaskTypeEnum Type { get; set; }

        /// <summary>
        /// Task instance status as seen by the Task Manager.
        /// </summary>
        public LongRunningTaskInstanceStatusEnum TaskInstanceStatus { get; set; }

        /// <summary>
        /// Task status.
        /// </summary>
        public LongRunningTaskStatus TaskStatus { get; set; }

        /// <summary>
        /// Order of tasks.
        /// </summary>
        public long Order { get; set; }

        /// <summary>
        /// Whether current task is uploading.
        /// </summary>
        public bool IsUpload { get; set; }

        /// <summary>
        /// Whether current task supports download.
        /// </summary>
        public bool IsDownload { get; set; }


        /// <summary>
        /// Ctor.
        /// </summary>
        public LongRunningTaskInstanceStatus()
        {
        }

        /// <summary>
        /// Ctor.
        /// </summary>
        public LongRunningTaskInstanceStatus(
            string id,
            string title,
            LongRunningTaskTypeEnum type,
            LongRunningTaskInstanceStatusEnum taskInstanceStatus,
            LongRunningTaskStatus taskStatus,
            long order,
            bool isUpload,
            bool isDownload)
        {
            this.Id = id;
            this.Title = title;
            this.Type = type;
            this.TaskInstanceStatus = taskInstanceStatus;
            this.TaskStatus = taskStatus;
            this.Order = order;
            this.IsUpload = isUpload;
            this.IsDownload = isDownload;
        }


        public bool Equals(LongRunningTaskInstanceStatus x, LongRunningTaskInstanceStatus y)
        {
            if (x == null && y == null)
                return true;

            if (x == null || y == null)
                return false;

            return ((x.Title == null && y.Title == null) || x.Title == y.Title)
                && x.Type == y.Type
                && x.TaskInstanceStatus == y.TaskInstanceStatus;
        }

        public int GetHashCode(LongRunningTaskInstanceStatus obj)
        {
            return this.Type.GetHashCode() | this.TaskInstanceStatus.GetHashCode();
        }
    }
}
